package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.ui.fragment.Search2Fragment;
import com.qilu.pe.ui.fragment.SearchFragment;

public class SearchActivity extends Base2Activity {
    private AppCompatEditText et_word;
    private RelativeLayout rl_back;
    private TextView tv_search;

    private XTabLayout tab_2;
    private ViewPager vp_tab;

    private VpTabAdapter vpTabAdapter;// tab
    private static final String[] HOME_MODELS = {"项目", "套餐"};

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_search;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        et_word = findViewById(R.id.et_word);
        tab_2 = findViewById(R.id.tab_2);
        vp_tab = findViewById(R.id.vp_tab);
        rl_back = findViewById(R.id.rl_back);
        tv_search = findViewById(R.id.tv_search);

        for (int i = 0; i < 2; i++) {
            tab_2.addTab(tab_2.newTab().setText(HOME_MODELS[i]), i);
        }
        vpTabAdapter = new VpTabAdapter(getSupportFragmentManager());
        vp_tab.setAdapter(vpTabAdapter);
        tab_2.setupWithViewPager(vp_tab);

        setListeners();
    }

    private void setListeners() {
        setOnClickListener(rl_back, tv_search);
        et_word.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                onClick(tv_search);
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == rl_back) {
            onBackPressed();
        } else if (v == tv_search) {
            if (null != onSearchClickListener) {
                String type = vp_tab.getCurrentItem() == 0 ? "1" : "2";// type 1 项目,2 套餐 (可为空)
                onSearchClickListener.onSearchClick(et_word.getText().toString().trim());
            }
            if (null != onSearchClickListener2) {
                String type = vp_tab.getCurrentItem() == 0 ? "1" : "2";// type 1 项目,2 套餐 (可为空)
                onSearchClickListener2.onSearchClick(et_word.getText().toString().trim());
            }
        }
    }

    // 预约下单 tab adapter
    private class VpTabAdapter extends FragmentPagerAdapter {

        public VpTabAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 1) {
                return Search2Fragment.newInstance("2");
            }
            return SearchFragment.newInstance("1");
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return HOME_MODELS[position];
        }
    }

    private OnSearchClickListener onSearchClickListener;

    public void setOnSearchClickListener(OnSearchClickListener onSearchClickListener) {
        this.onSearchClickListener = onSearchClickListener;
    }

    public interface OnSearchClickListener {
        void onSearchClick(String word);
    }

    private OnSearchClickListener2 onSearchClickListener2;

    public void setOnSearchClickListener2(OnSearchClickListener2 onSearchClickListener2) {
        this.onSearchClickListener2 = onSearchClickListener2;
    }

    public interface OnSearchClickListener2 {
        void onSearchClick(String word);
    }
}